package com.carlos.gateway.security.handler;

import com.carlos.gateway.security.JsonAuthentication;
import com.carlos.gateway.vo.ResultVO;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Carlos Li
 * @date 2021/5/23
 */

@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException {
        ResultVO<Void> result = new ResultVO<Void>().fail("用户无权限");
        JsonAuthentication.writeJson(httpServletResponse, result);
    }
}
